<template>
<div>
  <!-- 我的随工 -->
  <el-table
      :data="myaporder"
      stripe
      style="width: 100%"
      v-loading="loading">
      <el-table-column prop="aid" label="随工单号" width="110">
      </el-table-column>
      <el-table-column prop="apname" label="随工人员" width="80">
      </el-table-column>
      <el-table-column prop="oid" label="施工单号" width="110">
      </el-table-column>
      <el-table-column
        prop="starttime"
        label="施工开始时间"
        width="140px"
        >
      </el-table-column>
      <el-table-column prop="endtime" label="施工结束时间" width="140px">
      </el-table-column>
      <el-table-column prop="location" label="施工位置" width="120px">
      </el-table-column>
      <el-table-column prop="content" label="施工内容">
      </el-table-column>
      <el-table-column prop="fmsg" label="施工照片" width="180px">
        <template #default="scope">
          <el-image 
            style="width: 100px; height: 100px"
            :src="scope.row.fmsg" 
            :preview-src-list="[scope.row.fmsg]"
            preview-teleported="true">
        </el-image>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100px">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle @click="handleEdit(scope.row)"/>
        </template>
      </el-table-column>
    </el-table>

    
    
    <!-- 更新随工内容 -->
    <el-dialog
      :visible.sync="dialogVisible"
      width="50%">
  
      <el-form :model="ruleForm"  ref="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
        
        <el-form-item prop="aid" label="随工单号">
          <el-input  v-model.number="ruleForm.aid" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item prop="apname" label="随工人员姓名">
          <el-input  v-model="ruleForm.apname" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item prop="oid" label="施工单号" >
          <el-input  v-model.number="ruleForm.oid" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item prop="starttime" label="随工开始时间" >
          <el-input v-model="ruleForm.starttime" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="施工结束时间" prop="endtime">
          <el-date-picker
            v-model="ruleForm.endtime"
            type="datetime"
            placeholder="选择日期时间"
            value-format="yyyy-MM-dd HH:mm">
          </el-date-picker>
      </el-form-item>
      <el-form-item label="具体位置" prop="location">
        <el-input type="textarea" v-model="ruleForm.location"></el-input>
      </el-form-item>
      <el-form-item label="施工内容" prop="content">
        <el-input type="textarea" v-model="ruleForm.content"></el-input>
      </el-form-item>
      <el-form-item label="随工图片" prop="fmsg" >
        <el-upload
        ref="upload"
            action="http://localhost:8080/api/sys/aporder/fileUpload"
            :headers="{'token':token}"
            :on-success="uploadSuccess">
            <el-button type="primary">点击上传</el-button>
        </el-upload>
        <el-button type="danger" @click="attachmentRemove" :style="{ display: visible }">{{ this.ruleForm.fmsg }}</el-button>
      </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">确定</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
          <el-button type="success" @click="finish('ruleForm')">随工核验</el-button>
          <el-button @click="dialogVisible = false">取 消</el-button>
        </el-form-item>
      </el-form>
  
    </el-dialog>

    
</div>
</template>

<script>
import request from "../../utils/request";

export default {
  name: 'Supervising',
  data(){
    return{
      myaporder: [],
      ruleForm: {
      },
      visible: 'none',
      token: localStorage.getItem('Authorization'),
      loading: true,
      user: JSON.parse(localStorage.getItem('user')),
      dialogVisible: false,
      rules: {
        endtime: [
            { required: true, message: "请选择施工结束时间", trigger: 'blur' }
        ],
        location: [
            { required: true, message: "请填写施工具体位置", trigger: 'blur' }
        ],
        content: [
            { required: true, message: "请填写施工内容", trigger: 'blur' }
        ]}
    }
  },
  created() {
    this.load()
  },
  methods: {
    // 加载时将当前用户的随工记录展示出来
    load(){
      request.get("/sys/aporder/selectByPhone",{
        params:{phone: String(this.user.phone)
        }
      }).then(res => {
        
        console.log(res);
        this.myaporder = res.data
        this.loading = false
      })
      this.loading = false
    },
    attachmentRemove(){
        this.$refs['upload'].clearFiles()
        this.ruleForm.fmsg = ''
        this.visible = 'none'
    },
    uploadSuccess(res){
        console.log(res);
        this.ruleForm.fmsg = res.data
        this.visible = ''
    },
    // 将更新后的随工记录发送给后端更新
    submitForm(){
            this.ruleForm.phone = this.user.phone
            request.put("/sys/aporder/update",this.ruleForm).then(res =>{
          console.log(res);
          // console.log(this.ruleForm)
          if (res.code === '1'){
            this.$message({
              type: "success",
              message: "修改信息成功"
            })
            
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
          this.load() //刷新
          this.dialogVisible = false
        });
        this.load() //刷新
        this.dialogVisible = false
    },
    // 随工核验 将随工状态改为随工完成
    finish(formName){

      this.$refs[formName].validate((valid) => {
          if (valid && this.ruleForm.fmsg) {
            this.ruleForm.status = '随工完成'
      request.put("/sys/aporder/update",this.ruleForm).then(res =>{
          console.log(res);
          // console.log(this.ruleForm)
          if (res.code === '1'){
            this.$message({
              type: "success",
              message: "随工核验成功"
            })
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
          this.load() //刷新
          this.dialogVisible = false
        });
        this.load() //刷新
        this.dialogVisible = false
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      
    },
    //编辑 弹出信息更新表单
    handleEdit(row){
      this.ruleForm = JSON.parse(JSON.stringify(row))
        this.dialogVisible = true
        if(this.ruleForm.fmsg){
          this.visible = ''
        }
    },
    // 得到施工开始结束时间
    GetworkTime(){
      if (this.ruleForm.worktimerange != null){
        this.ruleForm.starttime = this.ruleForm.worktimerange[0];
        this.ruleForm.endtime = this.ruleForm.worktimerange[1];
      }
    },
      //重置新增 表单内容
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  }
}

</script>

<style>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

</style>
